package com.spic.business.informatization.meeting.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.spic.business.two.tickets.electricfirst.domain.ElectricFirstTicket;
import com.spic.business.two.tickets.electricsecond.domain.ElectricSecondTicket;
import com.spic.business.two.tickets.emergencyrepai.domain.EmergencyRepair;
import com.spic.business.two.tickets.firstfire.domain.FirstClassFireTicket;
import com.spic.business.two.tickets.general.domain.AddSafetyPrecautionsSubtable;
import com.spic.business.two.tickets.general.domain.PrecautionsSubtableMark;
import com.spic.business.two.tickets.general.domain.RiskControlMeasuresSubtable;
import com.spic.business.two.tickets.general.domain.SafetyPrecautionsSubtable;
import com.spic.business.two.tickets.general.eum.RiskEnum;
import com.spic.business.two.tickets.general.eum.TableCategoryEnum;
import com.spic.business.two.tickets.maintenance.domain.MaintenanceTicket;
import com.spic.business.two.tickets.operate.domain.OperateTicket;
import com.spic.business.two.tickets.secondclassfire.domain.SecondClassFireTicket;
import com.spic.business.two.tickets.thirdfire.domain.ThirdClassFireTicket;
import com.spic.common.core.exception.ServiceException;
import com.spic.common.core.utils.SecurityUtils;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.groupingBy;

@Data
public class MeetingActivityGroup {
    /**
     * 主键d
     */
    private Long id;

    /**
     * 三会一活动主键
     */
    @NotNull(message = "会议id关联不能为空")
    private Long meetingActivityId;

    /**
     * 组成员
     */
    private String members;

    /**
     * 是否需要票据
     */
    @NotBlank(message = "是否需要票据不为空")
    private String isRequiredTicket;

    /**
     * 票据类型
     */
    private String ticketType;

    /**
     * 票据主键id
     */
    private Long ticketId;

    /**
     * 开始时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date beginTime;

    /***
     * 结束时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date endTime;

    /**
     * 会议关联工作
     */
    List<MeetingActivityGroupWork> groupWorks;

    public boolean needTicket(){
        return "Y".equals(this.getIsRequiredTicket()) && this.getTicketType() != null;
    }

    public ElectricFirstTicket convertToElectricFirstTicket(){
        ElectricFirstTicket ticket = new ElectricFirstTicket();
        ticket.setWorkUnit(SecurityUtils.getDeptId()+"");
        ticket.setWorkGroup(this.getMembers());
        ticket.setWorkPrincipal(String.valueOf(SecurityUtils.getUserId()));
        ticket.setBeginTime(this.getBeginTime());
        ticket.setEndTime(this.getEndTime());
        initPrecautionsSubtable(ticket);
        return ticket;
    }

    public ElectricSecondTicket convertToElectricSecondTicket(){
        ElectricSecondTicket ticket = new ElectricSecondTicket();
        ticket.setWorkUnit(SecurityUtils.getDeptId()+"");
        ticket.setWorkGroup(this.getMembers());
        ticket.setBeginTime(this.getBeginTime());
        ticket.setEndTime(this.getEndTime());
        initPrecautionsSubtable(ticket);
        return ticket;
    }

    public OperateTicket convertToOperateTicket(){
        OperateTicket ticket = new OperateTicket();
        ticket.setOperator(this.getMembers());
        ticket.setBeginTime(this.getBeginTime());
        ticket.setEndTime(this.getEndTime());
        return ticket;
    }

    public EmergencyRepair convertToEmergencyRepair(){
        EmergencyRepair ticket = new EmergencyRepair();
        ticket.setWorkUnit(SecurityUtils.getDeptId()+"");
        ticket.setWorkGroup(this.getMembers());
        ticket.setBeginTime(this.getBeginTime());
        ticket.setEndTime(this.getEndTime());
        initPrecautionsSubtable(ticket);
        return ticket;
    }

    public FirstClassFireTicket convertToFirstClassFireTicket()
    {
        FirstClassFireTicket ticket = new FirstClassFireTicket();
        ticket.setWorkUnit(SecurityUtils.getDeptId()+"");
        ticket.setBeginTime(this.getBeginTime());
        ticket.setEndTime(this.getEndTime());
        initPrecautionsSubtable(ticket);
        return ticket;
    }

    public SecondClassFireTicket convertToSecondClassFireTicket(){
        SecondClassFireTicket ticket = new SecondClassFireTicket();
        ticket.setWorkUnit(SecurityUtils.getDeptId()+"");
        ticket.setBeginTime(this.getBeginTime());
        ticket.setEndTime(this.getEndTime());
        initPrecautionsSubtable(ticket);
        return ticket;
    }

    public ThirdClassFireTicket convertToThirdClassFireTicket(){
        ThirdClassFireTicket ticket = new ThirdClassFireTicket();
        ticket.setWorkUnit(SecurityUtils.getDeptId()+"");
        ticket.setBeginTime(this.getBeginTime());
        ticket.setEndTime(this.getEndTime());
        initPrecautionsSubtable(ticket);
        return ticket;
    }

    public MaintenanceTicket convertToMaintenanceTicket() {
        MaintenanceTicket ticket = new MaintenanceTicket();
        ticket.setWorkUnit(SecurityUtils.getDeptId()+"");
        ticket.setBeginTime(this.getBeginTime());
        ticket.setEndTime(this.getEndTime());
        initPrecautionsSubtable(ticket);
        return ticket;
    }

    private void initPrecautionsSubtable(PrecautionsSubtableMark ticket){
        List<MeetingActivityGroupWork> groupWork = this.getGroupWorks();
        if(CollectionUtils.isEmpty(groupWork))  return;
        Map<String, List<MeetingActivityGroupWork>> groupWorkMap = groupWork.stream().collect(groupingBy(MeetingActivityGroupWork::getRiskType));
        for(String key: groupWorkMap.keySet()){
            RiskEnum type = RiskEnum.valueOf(key);
            switch (type){
                case SAFETY_MEASURE:
                    List<SafetyPrecautionsSubtable> safetyMeasures = MeetingActivityGroupWork.convertToSafetyPrecautionsSubtable(groupWorkMap.get(key), TableCategoryEnum.FIRST_ELECTRIC);
                    ticket.setSafetyPrecautionsSubtableList(safetyMeasures);
                    break;
                case  CONTROL_MEASURE:
                    List<RiskControlMeasuresSubtable> riskMeasures = MeetingActivityGroupWork.convertToRiskControlMeasuresSubtable(groupWorkMap.get(key), TableCategoryEnum.FIRST_ELECTRIC);
                    ticket.setRiskControlMeasuresSubtableList(riskMeasures);
                    break;
                case  ADD_SAFETY_MEASURE:
                    List<AddSafetyPrecautionsSubtable> addSafety = MeetingActivityGroupWork.convertToAddSafetyPrecautionsSubtable(groupWorkMap.get(key), TableCategoryEnum.FIRST_ELECTRIC);
                    ticket.setAddSafetyPrecautionsSubtableList(addSafety);
                    break;
                default:
                    throw new ServiceException("创建两票风险类型错误");
            }
        }
    }


}

